<?php
/**
* @file $Id: PDFStop.fnc.php 639 2007-08-09 21:15:29Z ryness $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

/**
* function: PDFStop
*
*
* Currently recognized values of $options:
*	wrap_html	- Wrap the output buffer with HTML and BODY tags
*
*/

function PDFStop($handle, $options = array()) 
{	global $htmldocPath;

	// Test if should and can use PDFs
	if( ( ! empty($htmldocPath) ) && is_executable($htmldocPath) )
	{
		// Format the $options array with expected values
		$options['wrap_html'] = isset($options['wrap_html']) ?
			$options['wrap_html'] : true;

		unset($_SESSION['orientation']);
		$html = ereg_replace('=(["\'])?assets/',"=\\1http://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')).'/assets/',ob_get_contents());
		ob_end_clean();

		// Gen temp file and save contents
		$temphtml=tempnam('','html');
		//$temphtml_tmp=substr($temphtml, 0, strrpos($temphtml, ".")).'html';
		//rename($temphtml_tmp, $temphtml); 

		$fp=@fopen($temphtml,"w");
		if(!$fp)
			die("Can't open $temphtml");

		// If desired, wrap the buffer with the HTML and BODY tags
		if ($options['wrap_html'])
			fputs($fp,'<HTML><BODY>');
		
		// Output the contents of the buffer
		fputs($fp, $html);
		
		// If desired, wrap the buffer with the HTML and BODY tags
		if($options['wrap_html'])
			fputs($fp, '</BODY></HTML>');
		
		// Close the file
		@fclose($fp);

		// Set up for execution of HTMLDOC
		$htmldocPathRun = $htmldocPath;
		// If there is a space in the executable path and this is Windows, wrap in quotes.
		if( strpos($htmldocPath," ") !== FALSE )
		{
			if( (strpos($_SERVER['OS'],"Windows")!== FALSE) OR (strpos($_SERVER['SERVER_SOFTWARE'],"Microsoft") !== FALSE) )
				$htmldocPathRun = '"'.$htmldocPath.'"';	// Microsoft Windows needs quotations if there are spaces in the path
		}
		// If there is a space in the temporary file path, wrap it in quotes.
		if( strpos($temphtml," ") !== FALSE )
		{
			$temphtml = '"'.$temphtml.'"';
		}

		// No normal HTML can be emitted following this point because output is redirected to PDF "file".
		header("Cache-Control: public");
		header("Pragma: ");
		header("Content-Type: application/pdf");
		header("Content-Disposition: inline; filename=\"".str_replace('Print ','',ProgramTitle()).".pdf\"\n");

		////
		// Pass output control on to the htmlDoc program, then delete
		//	the resultant temporary file we created earlier
		////
		// Some systems may have HTMLDOC data in an odd place, so
		//   if a path to HTMLDOC data is specified then use it
		if( isset($htmldocDataPath) AND ! empty($htmldocDataPath) )
		{
			// If there is a space in the HTMLDOC datadir path, wrap it in quotes.
			if( strpos($htmldocDataPath," ") !== FALSE )
			{
				$htmldocDataPath = '"'.$htmldocDataPath.'"';
			}
			$htmldoccommand = $htmldocPathRun.' --datadir "'.$htmldocDataPath.'" '.$handle[options].' '.$temphtml;
			passthru( $htmldoccommand, $htmldoc_status);
		} else {
			$htmldoccommand = $htmldocPathRun.' '.$handle[options].' '.$temphtml.'';
			passthru( $htmldoccommand, $htmldoc_status);
		}
		@unlink($temphtml);
	} else {
		unset($_SESSION['orientation']);
	}
	unset($_REQUEST['_template_id']);

}
?>
